{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# numpy 统计\n",
    "- 求和：sum\n",
    "- 平均值：mean\n",
    "- 中位数：median\n",
    "- 标准差：std\n",
    "- 众数：mode\n",
    "- 极差：ptp\n",
    "- 最大值：max\n",
    "- 最小值：min\n",
    "- 统计非零元素个数：count_nonzero\n",
    "- 统计非零元素个数：nonzero\n",
    "- 统计元素个数：size"
   ],
   "id": "e93cd2446f703084"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-06T13:01:26.957905Z",
     "start_time": "2025-09-06T13:01:26.949009Z"
    }
   },
   "source": [
    "# 1.13模块编程. 求和\n",
    "import numpy as np\n",
    "a = np.array([1,2,3,4,5,6,7,11,21])\n",
    "print(np.sum(a))"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "60\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T13:01:28.625056Z",
     "start_time": "2025-09-06T13:01:28.619246Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2. 平均值\n",
    "print(np.mean(a))"
   ],
   "id": "dccf2b0b0aaf7e02",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.666666666666667\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T13:01:30.519556Z",
     "start_time": "2025-09-06T13:01:30.511294Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 3. 中位数\n",
    "print(np.median(a))\n",
    "# 数组的个数 奇数和偶数个\n",
    "# 奇数个：排序后中间的数值\n",
    "# 偶数个：排序后中间两个数的平均值"
   ],
   "id": "f7abdfe89c01a7d5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.0\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T13:08:46.205860Z",
     "start_time": "2025-09-06T13:08:46.191871Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 4. 标准差 和 方差\n",
    "print(np.var([1,2,3])) # 方差\n",
    "print(np.std([1,2,3])) # 标准差\n",
    "# 1.13模块编程,2,3 平均值=2\n",
    "# 方差：方差=[(1.13模块编程-2)^2+(2-2)^2+(3-2)^2]/3 = 0.6666\n",
    "# 标准差：标准差=sqrt(方差) = 0.8165\n",
    "fc = np.var(a)\n",
    "print(fc)\n",
    "print(np.sqrt(fc))\n",
    "print(np.std(a))\n",
    "\n",
    "# 判断分散程度\n",
    "# 标准差越小，数据越集中； 标准差越大，数据越分散\n",
    "# 第一步：求均值\n",
    "# 第二步：根据均值，求方差； 方差越大，数据越分散\n",
    "# 第三步：根据方差，求标准差； 标准差越小，数据越集中"
   ],
   "id": "1409cd41140306c8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6666666666666666\n",
      "0.816496580927726\n",
      "33.55555555555556\n",
      "5.792715732327589\n",
      "5.792715732327589\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T13:13:47.814779Z",
     "start_time": "2025-09-06T13:13:47.792245Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 5. 最大值和最小值\n",
    "max_num = np.max(a)\n",
    "print(max_num, np.argmax(a))\n",
    "min_num = np.min(a)\n",
    "print(min_num, np.argmin(a))"
   ],
   "id": "78c28250101dc588",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "21 8\n",
      "1 0\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T13:25:12.901762Z",
     "start_time": "2025-09-06T13:25:12.872889Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 6. 分位数\n",
    "# 回顾一下：中位数\n",
    "print(np.median([1,4,8,9]))\n",
    "print(np.median([1,4,8]))\n",
    "\n",
    "np.random.seed(0)\n",
    "arr = np.random.randint(0,100,4)\n",
    "print(arr)\n",
    "\n",
    "#-----------------\n",
    "#44   47   64   67\n",
    "print(np.median(arr))\n",
    "\n",
    "# 前百分几\n",
    "# 0.25*3=0.75\n",
    "# 0.75*(47-44) = 2.25 + 44 = 46.75\n",
    "print(np.percentile(arr,50))\n",
    "print(np.percentile(arr,25))\n",
    "print(np.percentile(arr,75))\n",
    "# 0.8*3 = 2.4\n",
    "# (67-64)*0.4 = 1.13模块编程.2 + 64 = 65.2\n",
    "print(np.percentile(arr,80))"
   ],
   "id": "23f620a5984e467d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.0\n",
      "4.0\n",
      "[44 47 64 67]\n",
      "55.5\n",
      "55.5\n",
      "46.25\n",
      "64.75\n",
      "65.2\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T13:31:48.548947Z",
     "start_time": "2025-09-06T13:31:48.538694Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 7.累积和、累积积\n",
    "arr = np.array([1,2,3])\n",
    "print(np.sum(arr)) # 求和\n",
    "print(np.cumsum(arr)) # 累积和\n",
    "# [1.13模块编程 3 6] 如何理解这个结果？ 累积 和，分开理解下。[1.13模块编程=1.13模块编程, 1.13模块编程+2=3, 1.13模块编程+2+3=6]\n",
    "print(np.cumprod(arr)) #累积 积"
   ],
   "id": "13186294e0feee4d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "[1 3 6]\n",
      "[1 2 6]\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T13:35:56.917150Z",
     "start_time": "2025-09-06T13:35:56.907416Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 8. 比较函数\n",
    "# 比较是否大于、小于、等于 、 逻辑与、或、非\n",
    "# 检查数组中是否有一个True，是否所有都是False，自定条件\n",
    "\n",
    "# 是否大于\n",
    "print(np.greater([3,4,5,6,7],4))\n",
    "# 是否小于\n",
    "print(np.less([3,4,5,6,7],4))\n",
    "# 是否等于\n",
    "print(np.equal([3,4,5,6,7],4))\n",
    "\n",
    "print(np.equal([3,4,5,6,7],[3,4,5,6,7])) # 同样的形状，对位进行比较\n"
   ],
   "id": "cbecaddead2bfe09",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False False  True  True  True]\n",
      "[ True False False False False]\n",
      "[False  True False False False]\n",
      "[ True  True  True  True  True]\n"
     ]
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T13:40:46.495344Z",
     "start_time": "2025-09-06T13:40:46.487739Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(np.logical_and([1,1],[0,1])) # 对位进行逻辑与\n",
    "print(np.logical_or([0,0],[1,0])) # 对位进行逻辑或\n",
    "print(np.logical_not([1,0])) # 逻辑非"
   ],
   "id": "92a3af8990610424",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False  True]\n",
      "[ True False]\n",
      "[False  True]\n"
     ]
    }
   ],
   "execution_count": 54
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T13:38:45.474804Z",
     "start_time": "2025-09-06T13:38:45.469784Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 检查元素是否至少有一个元素为True\n",
    "print(np.any([1,0,0]))\n",
    "print(np.any([0,0,0]))"
   ],
   "id": "dbc1ad16180b5197",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T13:39:20.814279Z",
     "start_time": "2025-09-06T13:39:20.808663Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 检查元素是否全部为True\n",
    "print(np.all([1,1,1]))\n",
    "print(np.all([1,0,1]))"
   ],
   "id": "4454cfd69141a3a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "execution_count": 49
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T13:46:29.897622Z",
     "start_time": "2025-09-06T13:46:29.887216Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 自定义条件\n",
    "arr = np.array([1,2,3,4,5,6,7,8,9])\n",
    "print(np.any(arr>5))\n",
    "\n",
    "print(np.where(arr>5))\n",
    "# np.where(条件，符合条件，不符合条件)\n",
    "print(np.where(arr>5, arr, 0))\n",
    "print(np.where(arr<3, arr, 0)) # 不符合条件的元素全置为0\n",
    "\n",
    "print(np.where(arr<3,1,0)) # 符合条件置为1，不符合条件的置为0"
   ],
   "id": "465b6137394a6f6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "(array([5, 6, 7, 8]),)\n",
      "[0 0 0 0 0 6 7 8 9]\n",
      "[1 2 0 0 0 0 0 0 0]\n",
      "[1 1 0 0 0 0 0 0 0]\n"
     ]
    }
   ],
   "execution_count": 72
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T13:50:59.465184Z",
     "start_time": "2025-09-06T13:50:59.438836Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 学生分数---案例\n",
    "score = np.random.randint(50,100,20)\n",
    "print(score)\n",
    "print(np.where(score>=90, '优秀', score))\n",
    "print(np.where(\n",
    "    score>=90, '优秀', np.where(\n",
    "        score>=80, '良好', np.where(\n",
    "            score>=70, '及格', '不及格'\n",
    "        )\n",
    "    )\n",
    "))\n",
    "\n",
    "select_arr = np.select([score>=90, score>=80, score>=70], ['优秀', '良好', '及格'], default='不及格')\n",
    "print(select_arr)"
   ],
   "id": "882f45316f094059",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[81 51 51 89 91 85 88 61 96 68 77 50 64 85 62 92 70 61 54 56]\n",
      "['81' '51' '51' '89' '优秀' '85' '88' '61' '优秀' '68' '77' '50' '64' '85'\n",
      " '62' '优秀' '70' '61' '54' '56']\n",
      "['良好' '不及格' '不及格' '良好' '优秀' '良好' '良好' '不及格' '优秀' '不及格' '及格' '不及格' '不及格'\n",
      " '良好' '不及格' '优秀' '及格' '不及格' '不及格' '不及格']\n",
      "['良好' '不及格' '不及格' '良好' '优秀' '良好' '良好' '不及格' '优秀' '不及格' '及格' '不及格' '不及格'\n",
      " '良好' '不及格' '优秀' '及格' '不及格' '不及格' '不及格']\n"
     ]
    }
   ],
   "execution_count": 76
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T13:56:48.730174Z",
     "start_time": "2025-09-06T13:56:48.714024Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 9. 排序函数\n",
    "np.random.seed(0)\n",
    "arr = np.random.randint(1,100,20)\n",
    "print(arr)\n",
    "print(np.sort(arr))\n",
    "print(np.argsort(arr))\n",
    "print(arr.argsort())\n",
    "# arr.sort() # 改变了原始数组arr的顺序\n",
    "# print(arr[5])"
   ],
   "id": "5178103d74198c3c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[45 48 65 68 68 10 84 22 37 88 71 89 89 13 59 66 40 88 47 89]\n",
      "[10 13 22 37 40 45 47 48 59 65 66 68 68 71 84 88 88 89 89 89]\n",
      "[ 5 13  7  8 16  0 18  1 14  2 15  3  4 10  6  9 17 11 12 19]\n",
      "[ 5 13  7  8 16  0 18  1 14  2 15  3  4 10  6  9 17 11 12 19]\n"
     ]
    }
   ],
   "execution_count": 83
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T13:57:38.928213Z",
     "start_time": "2025-09-06T13:57:38.918514Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 10. 去重函数\n",
    "print(np.unique(arr)) # 发现：去重后，还进行排序"
   ],
   "id": "266c258bd876ffd8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 13 22 37 40 45 47 48 59 65 66 68 71 84 88 89]\n"
     ]
    }
   ],
   "execution_count": 84
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T13:59:01.547544Z",
     "start_time": "2025-09-06T13:59:01.538990Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数组的拼接\n",
    "arr1 = np.array([1,2,3])\n",
    "arr2 = np.array([4,5,6])\n",
    "print(np.concatenate([arr1,arr2])) # 拼接\n",
    "print(arr1 + arr2) # 运算,对位相加"
   ],
   "id": "6c6be450d69f9afa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n",
      "[5 7 9]\n"
     ]
    }
   ],
   "execution_count": 86
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T14:03:02.609336Z",
     "start_time": "2025-09-06T14:03:02.603022Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数组的分割\n",
    "print(arr)\n",
    "print(np.split(arr,2))\n",
    "print(np.size(arr)) # 20\n",
    "# print(np.split(arr,3)) # 3个等分,报错，因为arr有20个元素，不可进行三等分\n",
    "print(np.split(arr,5))"
   ],
   "id": "c432cd446c29bf68",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[45 48 65 68 68 10 84 22 37 88 71 89 89 13 59 66 40 88 47 89]\n",
      "[array([45, 48, 65, 68, 68, 10, 84, 22, 37, 88]), array([71, 89, 89, 13, 59, 66, 40, 88, 47, 89])]\n",
      "20\n",
      "[array([45, 48, 65, 68]), array([68, 10, 84, 22]), array([37, 88, 71, 89]), array([89, 13, 59, 66]), array([40, 88, 47, 89])]\n"
     ]
    }
   ],
   "execution_count": 94
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-06T14:08:21.445284Z",
     "start_time": "2025-09-06T14:08:21.440227Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 回顾\n",
    "\n",
    "print(np.empty([2,3],dtype=int))\n",
    "#print(np.zeros([2,3]))\n",
    "#print(np.ones([2,3]))\n",
    "#print(np.eye(3))\n",
    "#print(np.full([2,3],5))"
   ],
   "id": "ecfdcef2c626a88d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4607182418800017408 4607182418800017408 4607182418800017408]\n",
      " [4607182418800017408 4607182418800017408 4607182418800017408]]\n"
     ]
    }
   ],
   "execution_count": 156
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
